package com.shqcjd.yunweiyanoperator.widget;

import android.annotation.TargetApi;
import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.savvi.rangedatepicker.CalendarPickerView;
import com.shqcjd.yunweiyanoperator.R;
import com.shqcjd.yunweiyanoperator.util.DateUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class DateDialog extends Dialog {
    private Context context;
    private setOnCheckDateListener listener;
    CalendarPickerView calendar;
    private Button btn_dialog_date;

    public void setOnCheckDate(setOnCheckDateListener listener){
        this.listener = listener;
    }

    public DateDialog(@NonNull Context context) {
        super(context);
        this.context = context;
    }

    public DateDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected DateDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @TargetApi(Build.VERSION_CODES.O)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_date_pick);
        getWindow().setBackgroundDrawableResource(R.color.transparent);
        final Calendar nextYear = Calendar.getInstance();
        nextYear.add(Calendar.YEAR, 10);

        final Calendar lastYear = Calendar.getInstance();
        lastYear.add(Calendar.YEAR, -10);
        calendar = (CalendarPickerView) findViewById(R.id.calendar_view);
        btn_dialog_date = (Button) findViewById(R.id.btn_dialog_date);
//        final ArrayList<Integer> list = new ArrayList<>();
//        list.add(1);

//        calendar.deactivateDates(list);
        ArrayList<Date> arrayList = new ArrayList<>();
        calendar.init(lastYear.getTime(), nextYear.getTime(), new SimpleDateFormat("MMMM, yyyy", Locale.getDefault())) //
                .inMode(CalendarPickerView.SelectionMode.RANGE) //
                .withSelectedDate(new Date())
//                .withDeactivateDates(list)
                .withHighlightedDates(arrayList);
        btn_dialog_date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener==null){
                    return;
                }
                List<Date> l = calendar.getSelectedDates();
                if(l.size()==1){
                    listener.onCheckDate(DateUtil.getDateToString(calendar.getSelectedDates().get(0).getTime(),"yyyy/MM/dd"),
                            DateUtil.getDateToString((calendar.getSelectedDates().get(0).getTime()+86400000),"yyyy/MM/dd"));
                    dismiss();
                }else if(l.size()>1){
                    listener.onCheckDate(DateUtil.getDateToString(calendar.getSelectedDates().get(0).getTime(),"yyyy/MM/dd"),
                            DateUtil.getDateToString(calendar.getSelectedDates().get(calendar.getSelectedDates().size()-1).getTime(),"yyyy/MM/dd"));
                    dismiss();
                }else{
                    Toast.makeText(context, "请选择时间", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public interface setOnCheckDateListener{
        void onCheckDate(String time, String endTime);
    }
}
